// pages/addresslist/addresslist.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: "",
    addindex:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getAddress()

  },
  async addip() {
    // 获取地址
    let address = wx.getStorageSync("address")
    let res = await wx.chooseAddress()
    if (res.errMsg.includes("ok")) {
      this.setData({
        address: res
      })
    }
    if (address) {
      address.unshift(this.data.address)
      wx.setStorageSync('address', address)
    } else {
      wx.setStorageSync('address', [{
        ...this.data.address
      }])
    }
  },
  getAddress() {
    let address = wx.getStorageSync('address')
    this.setData({
      address: address
    })
  },
  changeAddress(e){
     let index=e.currentTarget.dataset.index;
     let addlist=wx.getStorageSync('address')
     let wasChosen=addlist[index]
     addlist.splice(index,1)
     addlist.unshift(wasChosen)
     this.setData({
       addindex:index
     })
     wx.setStorageSync('address', addlist)
     wx.navigateBack()
  },
  // 编辑地址
  editAddress(e){
    console.log(e);
    let index=e.currentTarget.dataset.index
    wx.navigateTo({
      url: `../addressedit/edit?index=${index}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAddress()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})